﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using TestXNA3.Components;
using Microsoft.Xna.Framework;
using System.Runtime.Serialization;

namespace TestXNA3.Actors
{
    [Serializable]
    public abstract class Light : Actor
    {
        private Color _diffuse = Color.White;
        [System.ComponentModel.Category("Light")]
        public Color Diffuse
        {
            get { return _diffuse; }
            set { _diffuse = value; }
        }

        private Color _specular = Color.White;
        [System.ComponentModel.Category("Light")]
        public Color Specular
        {
            get { return _specular; }
            set { _specular = value; }
        }

        private float _intensity = 1.0f;
        [System.ComponentModel.Category("Light")]
        public float Intensity
        {
            get { return _intensity; }
            set { _intensity = value; }
        }
        private bool _castShadows = true;
        [System.ComponentModel.Category("Light")]
        public bool CastShadows
        {
            get { return _castShadows; }
            set { _castShadows = value; }
        }

        protected SpriteComponent _spriteComponent;

        public Light()
        {
            _saveable = true;
            BoundingBoxSize = new Vector3(16, 16, 16);
        }

        private void saveColor(Color c, System.IO.BinaryWriter writer)
        {
            writer.Write(c.R);
            writer.Write(c.G);
            writer.Write(c.B);
            writer.Write(c.A);
        }

        protected override void duplicateProperties(Actor a)
        {
            base.duplicateProperties(a);
            Light l = a as Light;
            l.Diffuse = Diffuse;
            l.Specular = Specular;
            l.Intensity = Intensity;
        }

        private Color loadColor(System.IO.BinaryReader reader)
        {
            Color c = Color.Wheat;
            c.R = reader.ReadByte();
            c.G = reader.ReadByte();
            c.B = reader.ReadByte();
            c.A = reader.ReadByte();
            return c;
        }

        public override void Save(System.IO.BinaryWriter writer)
        {
            base.Save(writer);
            saveColor(Diffuse, writer);
            saveColor(Specular, writer);
            writer.Write(Intensity);
        }

        public override void Load(System.IO.BinaryReader reader)
        {
            Components.Clear();
            base.Load(reader);
            Diffuse = loadColor(reader);
            Specular = loadColor(reader);
            Intensity = reader.ReadSingle();
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue("Diffuse", Diffuse);
            info.AddValue("Specular", Specular);
            info.AddValue("Intensity", Intensity);
            info.AddValue("CastShadows", CastShadows);
        }
    }
}
